package com.kaifamiao.demo.d0907;

import java.sql.*;
import java.util.Date;

public class Demo {


    public static void main(String[] args) {
    }

    public static void query(){
        try {
            // 注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/kfm";
        String sql = "select * from emp";
        try( // 创建连接
             Connection conn = DriverManager.getConnection(url, "root", "");
             PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ) {
            // 执行
            System.out.println(preparedStatement.executeQuery());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void create(int empNo, String eName, Date date){
        try {
            // 注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/kfm";
        String sql = "INSERT INTO EMP (empno, ename, hiredate) values(?, ?, ?)";
        try( // 创建连接
             Connection conn = DriverManager.getConnection(url, "root", "");
             PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ) {

            // 设置参数
            preparedStatement.setInt(1, empNo);
            preparedStatement.setString(2,eName);
            preparedStatement.setObject(3, date);

            // 执行
            System.out.println(preparedStatement.executeUpdate());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
